/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sample.util;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import sample.controller.MyController;
import sample.model.MonitorFragmentModel;
import sample.model.ZDYdataModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static sample.util.SSHUtils.execCommandByJSch;


/**
 *
 * @author wutc
 */
public class GetZDYdata implements Callable<ZDYdataModel> {

//    private String[] MonitorFragmentInfo = new String[9];

    private HashMap<String, Session> serverSessionMap = new HashMap<String, Session>();
    private HashMap<String, String[]> serverMap = new HashMap<String, String[]>();
    private static Boolean flag ;
    private Iterator it ;
    private int mapsize ;
    private String[] keys;
    private ZDYdataModel zdYdataModel;
    private String myCommand1,myCommand2,myCommand3,myCommand4;
    private Boolean isMyCommand1,isMyCommand2,isMyCommand3,isMyCommand4;
    int keynum ;
//    DefaultTableModel model = new DefaultTableModel();

    public GetZDYdata(HashMap serverMap, HashMap serverSessionMap, int keynum){
        this.serverMap = serverMap;
        this.serverSessionMap = serverSessionMap;
        this.mapsize = serverSessionMap.size();
        this.it = serverSessionMap.keySet().iterator();
        this.keynum = keynum;
        this.flag = true;
        this.keys = new String[mapsize];
        for (int i = 0; i < mapsize; i++) {
            keys[i] = it.next().toString();
        }
    }

    public void SetMyCommand(Boolean is1,Boolean is2,Boolean is3,Boolean is4,String my1,String my2,String my3,String my4){
        this.isMyCommand1 = is1;
        this.isMyCommand2 = is2;
        this.isMyCommand3 = is3;
        this.isMyCommand4 = is4;
        this.myCommand1 = my1;
        this.myCommand2 = my2;
        this.myCommand3 = my3;
        this.myCommand4 = my4;
    }



    @Override
    public ZDYdataModel call() {
        String[] ZDYdataModel2 =  new String[10];
        while (flag){
            try {
                ZDYdataModel2 = getinfo();
                zdYdataModel = new ZDYdataModel(ZDYdataModel2[0],ZDYdataModel2[1],ZDYdataModel2[2],ZDYdataModel2[3],ZDYdataModel2[4],ZDYdataModel2[5]);
            }catch (Exception e){
                System.out.println("连接异常:"+e.getMessage()+"请重新连接再试");
            }
        }
        return zdYdataModel;
    }

     private String[] getinfo() throws IOException, JSchException, InterruptedException {
        String[] ZDYdataInfo = new String[6];
        String str = getMapskey();
        //如果serverInfo中该ip设定为忽略则不执行命令
         if(Integer.parseInt(serverMap.get(str)[4])==1){
             //判断serverSessionMap 中是否还存在数据
             if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("LINUX")){
                 ZDYdataInfo[0] = serverMap.get(str)[0];
                 ZDYdataInfo[1] = str;
                 if(isMyCommand1){
                     ZDYdataInfo[2] = execCommandByJSch(serverSessionMap.get(str), myCommand1, "UTF-8");
                 }else {
                     ZDYdataInfo[2] = "";
                 }
                 if(isMyCommand2){
                     ZDYdataInfo[3] = execCommandByJSch(serverSessionMap.get(str), myCommand2, "UTF-8");
                 }else {
                     ZDYdataInfo[3] = "";
                 }
                 if(isMyCommand3){
                     ZDYdataInfo[4] = execCommandByJSch(serverSessionMap.get(str), myCommand3, "UTF-8");
                 }else {
                     ZDYdataInfo[4] = "";
                 }
                 if(isMyCommand4){
                     ZDYdataInfo[5] = execCommandByJSch(serverSessionMap.get(str), myCommand4, "UTF-8");
                 }else {
                     ZDYdataInfo[5] = "";
                 }
             }
             if(execCommandByJSch(serverSessionMap.get(str), "uname", "UTF-8").toUpperCase().contains("AIX")){
                 ZDYdataInfo[0] = serverMap.get(str)[0];
                 ZDYdataInfo[1] = str;
                 ZDYdataInfo[2] = execCommandByJSch(serverSessionMap.get(str), "prtconf|grep Processors|awk -F \" \" '{print $4}'", "UTF-8") + "/" +  " "+ "/" + execCommandByJSch(serverSessionMap.get(str), "vmstat|grep lcpu|awk -F \"=\" '{print $2}'|awk -F \" \" '{print $1}'", "UTF-8");
                 ZDYdataInfo[3] = execCommandByJSch(serverSessionMap.get(str), "vmstat |grep lcpu|awk -F \"=\" '{print $3}'", "UTF-8");
                 ZDYdataInfo[4] = execCommandByJSch(serverSessionMap.get(str), "df -g|awk '{print $6,$7}'|sort -rn|head -1", "UTF-8");
                 ZDYdataInfo[5] = execCommandByJSch(serverSessionMap.get(str), "java -version 2>&1 | sed '1!d' | sed -e 's/\"//g' | awk '{print $3}'", "UTF-8");
             }
         }else if(Integer.parseInt(serverMap.get(str)[4])==0){
             ZDYdataInfo[0] = serverMap.get(str)[0];
             ZDYdataInfo[1] = str;
             ZDYdataInfo[2] = "-";
             ZDYdataInfo[3] = "-";
             ZDYdataInfo[4] = "-";
             ZDYdataInfo[5] = "-";
         }

            Thread.sleep(50);
         //System.out.println("线程"+Thread.currentThread().getName()+"获取了服务器数据"+",此时的 keynum是:"+keynum );
        return ZDYdataInfo;
    }

    public synchronized String getMapskey(){
        String key = keys[keynum];
        keynum++;
        if(keynum >= mapsize){
            flag = false;
        }
       // System.out.println("线程"+Thread.currentThread().getName()+"获取了key"+key+",此时的 keynum已经是:"+keynum );
        return key;
    }
    //去掉一个字符串中的换行符、回车符等，将连续多个空格替换成一个空格
    public static String ResultFormat(String string){
        Pattern p = Pattern.compile("\t|\r|\n");
        Matcher m = p.matcher(string);
        string = m.replaceAll("");
        string = string.replaceAll(" +", " ");
        return string;
    }



}


